#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_DeleteOnClose);
    this->setWindowTitle(tr("AGV仓储系统"));

    //加载翻译文件
//    ESS_Single::GetInstance()->trans.load("./Translate_VI.qm");
//    ESS_Single::GetInstance()->_language = "VI";
//    ESS_Single::GetInstance()->Map_write();
//    qApp->installTranslator(&ESS_Single::GetInstance()->trans);

    ui->retranslateUi(this);
//    WidgetInit(true);

    //主界面翻译信号
//    emit sig_Translate();

    NewClassItiem();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::WidgetInit(bool Visible)
{
    if(Visible){
        //this->setWindowFlags(Qt::FramelessWindowHint);    // 设置窗口标志
        this->setWindowState(Qt::WindowMaximized);
        this->setStyleSheet(tr("border:0xp"));
        this->showMaximized();

        ui->widget->setVisible(true);
        IconHelper::Instance()->setIcon(ui->lab_Ico, QChar(0xf015), 22);
        IconHelper::Instance()->setIcon(ui->btn_log, QChar(0xf05a), 20);
        IconHelper::Instance()->setIcon(ui->btn_Edite, QChar(0xf0c9), 18);
        IconHelper::Instance()->setIcon(ui->btn_Device, QChar(0xf0e8), 18);
    }else{
        //获取可用桌面大小
        QRect deskRect =  QApplication::desktop()->availableGeometry(this);
        this->resize(int(deskRect.width()*0.8),int(deskRect.height()*0.75));
        ui->widget->setVisible(false);
        //this->setWindowFlags(Qt::FramelessWindowHint);    // 设置窗口标志
    }
}

void MainWindow::NewClassItiem()
{
   loginItiem=new Login;
//   AckNormalItem=new AckNormal;
   ManagerInfoItem=new ManagerInfo;         //菜单管理


   /**
    * 赖俊龙
    * 取消主窗口日志对象
    */
//   dailyRecordItem=new dailyRecord;         //日志

   desktopServerI=new desktopServer;        //主页
   threaddesktopServer=new QThread();
   desktopServerI->moveToThread(threaddesktopServer);
   threaddesktopServer->start();

   managerServerI=new managerServer;          //设备管理
   threaddeviceServer=new QThread();
   managerServerI->moveToThread(threaddeviceServer);
   threaddeviceServer->start();

   taskManagerServerI=new taskManagerServer;//任务管理
   threadtaskManagerServer=new QThread();
   taskManagerServerI->moveToThread(threadtaskManagerServer);
   threadtaskManagerServer->start();

   threadServerI=new threadServer;
   threadServerItem=new QThread();
   threadServerI->moveToThread(threadServerItem);
   threadServerItem->start();

//   agvCarPool1=new agvCarPool;
//   agvCarPool1->listen(QHostAddress::Any,9001);
//   ThreadagvCarPool=new QThread();
//   agvCarPool1->moveToThread(ThreadagvCarPool);
//   ThreadagvCarPool->start();
   //agvCarPool1->init();

   ui->stackedWidget->insertWidget(0,loginItiem);
   ui->stackedWidget->insertWidget(1,desktopServerI->MainDesktopItiem);     //主页


   /**
    * 赖俊龙
    * 取消主窗口日志界面
    */
//   ui->stackedWidget->insertWidget(2,dailyRecordItem);                      //日志界面


   ui->stackedWidget->insertWidget(3,ManagerInfoItem);                      //菜单管理
   ui->stackedWidget->insertWidget(4,managerServerI->DeviceManageItem);     //设备管理

   ui->stackedWidget->setCurrentIndex(0);
   ui->stackedWidget->widget(0)->layout()->setContentsMargins(0,0,0,0);
   ui->stackedWidget->widget(1)->layout()->setContentsMargins(0,0,0,0);

   WidgetInit(false);
   CreateConnect();

}

void MainWindow::CreateConnect()
{

    /******************** 系统登录页面 *******************/
    connect(loginItiem,&Login::UserLogin,this,&MainWindow::ON_UserLogin);

    /***************** 业务与系统任务状态逻辑部分 **********/
    //connect(taskManagerServerI,&taskManagerServer::sigAGVMoveTask,agvCarPool1,&agvCarPool::onAGVMoveTask);
    //connect(taskManagerServerI,&taskManagerServer::sigChargeTask,agvCarPool1,&agvCarPool::ON_AGVChargeTask);

    /*****************   主页显示部分 *******************/
    //connect(desktopServerI,&desktopServer::sigAGVMoveTask,agvCarPool1,&agvCarPool::onAGVMoveTask);
    connect(desktopServerI,&desktopServer::newAGVMoveTask,taskManagerServerI,&taskManagerServer::ON_AGVMoveTask);

    connect(desktopServerI->MainDesktopItiem,&MainDesktop::sigAgvTaskBool,taskManagerServerI,&taskManagerServer::ON_AgvTaskBool);
    connect(desktopServerI->MainDesktopItiem,&MainDesktop::sig_UPSAPExcelInfoTask,taskManagerServerI,&taskManagerServer::ON_UPSAPExcelInfoTask);
    connect(desktopServerI->MainDesktopItiem,&MainDesktop::sig_AGVCurrentTaskAction,taskManagerServerI,&taskManagerServer::ON_AGVCurrentTaskAction);

    connect(desktopServerI->MainDesktopItiem,&MainDesktop::sig_deleteSAPExcelInfoTask,taskManagerServerI,&taskManagerServer::ON_deleteSAPExcelInfoTask);

    /********************** 东莞拓安信物联 ********************/
    connect(desktopServerI->MainDesktopItiem,&MainDesktop::sig_WriteModbusII,taskManagerServerI,&taskManagerServer::ON_WriteModbusII);//PLC 监控触发 Modbus 写入
    connect(desktopServerI->MainDesktopItiem,&MainDesktop::sig_UpdateStationI,taskManagerServerI,&taskManagerServer::UpdateStation_Show);//库位显示界面 更新库位状态
//    connect(this,&MainWindow::sig_Translate,desktopServerI->MainDesktopItiem,&MainDesktop::ON_Translate);//主界面翻译信号
//    connect(this,&MainWindow::sig_Translate,ManagerInfoItem,&ManagerInfo::ON_Translate);//主界面翻译信号
//    connect(this,&MainWindow::sig_Translate,loginItiem,&Login::ON_Translate);//主界面翻译信号
    connect(taskManagerServerI,&taskManagerServer::sig_TextDisplay,desktopServerI->MainDesktopItiem,&MainDesktop::ON_TextDisplay);//提示文本描述显示

    /*****************   菜单管理部分 *******************/
    connect(ManagerInfoItem->taskmanagerItem,&taskManager::sig_SAPExcelInfo_new,taskManagerServerI,&taskManagerServer::ON_SAPExcelInfo_new);
    connect(ManagerInfoItem->taskmanagerItem,&taskManager::sig_deleteSAPExcelInfoTask,taskManagerServerI,&taskManagerServer::ON_deleteSAPExcelInfoTask);
    connect(taskManagerServerI,&taskManagerServer::sigAGVCurrentTaskMap,desktopServerI->MainDesktopItiem,&MainDesktop::slotAGVCurrentTaskMap);


    /**
    * 赖俊龙
    * 取消主窗口主界面监控槽函数绑定
    */
    //   connect(taskManagerServerI,&taskManagerServer::sig_CodeStatus,desktopServerI->MainDesktopItiem->monitoringI,&monitoring::ON_CodeStatus);


    connect(taskManagerServerI,&taskManagerServer::sig_CodeStatus,managerServerI->DeviceManageItem->stationItemI,&stationItem::ON_CodeStatus);

    /**************** AGV状态反馈以及显示  ***************/
    //connect(agvCarPool1,&agvCarPool::AgvStateWidget,desktopServerI,&desktopServer::AgvStateWidget);
    //connect(agvCarPool1,&agvCarPool::AgvStateWidget,taskManagerServerI,&taskManagerServer::ON_AgvStateWidget);

    /******************** 接驳台部分 *******************/
    connect(taskManagerServerI,&taskManagerServer::sigDeviceStatus, managerServerI->DeviceManageItem->stationItemI,&stationItem::ON_DeviceStatus);

}

void MainWindow::ON_UserLogin(QString UserNuber,QString UserName)
{
    WidgetInit(true);
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_lab_Ico_clicked()
{
    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_btn_log_clicked()
{
    /**
     * 赖俊龙
     * 隐藏日志界面
     */
//    ui->stackedWidget->setCurrentIndex(2);
}

void MainWindow::on_btn_Edite_clicked()
{
    ui->stackedWidget->setCurrentIndex(3);
}

void MainWindow::on_btn_Device_clicked()
{
    /**
     * 赖俊龙
     * 隐藏设备管理界面
     */
//     ui->stackedWidget->setCurrentIndex(4);
}

void MainWindow::on_action_CN_triggered()
{
    //加载翻译文件
    ESS_Single::GetInstance()->trans.load("./Translate_CH.qm");
    ESS_Single::GetInstance()->_language = "CH";
    ESS_Single::GetInstance()->Map_write();
    qApp->installTranslator(&ESS_Single::GetInstance()->trans);
    ui->retranslateUi(this);
    WidgetInit(true);

    //主界面翻译信号
    emit sig_Translate();
}

void MainWindow::on_action_VI_triggered()
{
    //加载翻译文件
    ESS_Single::GetInstance()->trans.load("./Translate_VI.qm");
    ESS_Single::GetInstance()->_language = "VI";
    ESS_Single::GetInstance()->Map_write();
    qApp->installTranslator(&ESS_Single::GetInstance()->trans);
    ui->retranslateUi(this);
    WidgetInit(true);

    //主界面翻译信号
    emit sig_Translate();
}
